local module = {}
module.name = "Buffs"
module.Init = function()
    if not SCDB.modules[module.name] then return end
    if SCDB[module.name] == nil then SCDB[module.name] = {} end
	if SCDB[module.name]["Gradient"] == nil then SCDB[module.name]["Gradient"] = true end

	for _, frame in pairs({
		"BuffFrame",
		"TemporaryEnchantFrame",
		"ConsolidatedBuffs",
	}) do
		_G[frame]:SetScript("OnUpdate", nil)
		_G[frame]:UnregisterAllEvents()
		_G[frame]:Hide()
	end

	local function FormatTime(s)
		if (s < 3600 and s >= 60) then
			return format("%dm", floor(s / 60 + 1))
		elseif (s < 1) then
			return format("%.1f", s)
		elseif (s < 60) then
			return floor(s+1)
		else
			return format("%dh", floor(s/ 3600 + 1))
		end
	end

	local function UpdateTime(self, elapsed)
		if(self.expiration) then	
			self.expiration = math.max(self.expiration - elapsed, 0)
			if(self.expiration <= 0) then
				self.time:SetText("")
			else
				local time = FormatTime(self.expiration)
				if self.expiration <= 86400.5 and self.expiration > 3600.5 then
					self.time:SetText("|cffcccccc"..time.."|r")
				elseif self.expiration <= 3600.5 and self.expiration > 60.5 then
					self.time:SetText("|cffcccccc"..time.."|r")
				elseif self.expiration <= 60.5 and self.expiration > 10.5 then
					self.time:SetText("|cffE8D911"..time.."|r")
				elseif self.expiration <= 10.5 then
					self.time:SetText("|cffff0000"..time.."|r")
				end
			end
		end
	end

	local function UpdateWeapons(button, slot, active, expiration)
		if not button.texture then
			button.texture = button:CreateTexture(nil, "BORDER")
			button.texture:SetPoint("TOPLEFT",2,-2)
			button.texture:SetPoint("BOTTOMRIGHT",-2,2)
			
			button.time = button:CreateFontString(nil, "ARTWORK")
			button.time:SetPoint("BOTTOM", 0, -12)
			button.time:SetFontObject(GameFontHighlight)
					
			button.bg = button:CreateTexture(nil, "OVERLAY")
			button.bg:SetAllPoints(button)
			button.bg:SetTexture("Interface\\Buttons\\UI-TotemBar")
			button.bg:SetTexCoord(1 / 128, 35 / 128, 207 / 256, 240 / 256)
			button.bg:SetDesaturated(1)
			button.bg:SetVertexColor(1,1,1,1)
			if SCDB[module.name]["Gradient"] == true then
				button.bg:SetGradientAlpha("VERTICAL", .4, .4, .4, 1, 1, 1, 1, 1)
			end
		end
		
		if active then
			button.id = GetInventorySlotInfo(slot)
			button.icon = GetInventoryItemTexture("player", button.id)
			button.texture:SetTexture(button.icon)
			button.texture:SetTexCoord(0.08, 0.92, 0.08, 0.92)		
			button.expiration = (expiration/1000)
			button:SetScript("OnUpdate", UpdateTime)		
		elseif not active then
			button.texture:SetTexture(nil)
			button.time:SetText("")
			button:SetScript("OnUpdate", nil)
		end
	end

	local function UpdateAuras(header, button, weapon)
		if(not button.texture) then
			button.texture = button:CreateTexture(nil, "BORDER")
			button.texture:SetPoint("TOPLEFT",2,-2)
			button.texture:SetPoint("BOTTOMRIGHT",-2,2)

			button.count = button:CreateFontString(nil, "ARTWORK")
			button.count:SetPoint("BOTTOMRIGHT", -1, 1)
			button.count:SetFontObject(GameFontHighlight)

			button.time = button:CreateFontString(nil, "ARTWORK")
			button.time:SetPoint("BOTTOM", 0, -13)
			button.time:SetFontObject(GameFontHighlight)
			
			button:SetScript("OnUpdate", UpdateTime)
			
			button.bg = button:CreateTexture(nil, "OVERLAY")
			button.bg:SetAllPoints(button)
			button.bg:SetTexture("Interface\\Buttons\\UI-TotemBar")
			button.bg:SetTexCoord(1 / 128, 35 / 128, 207 / 256, 240 / 256)
			button.bg:SetDesaturated(1)
			button.bg:SetVertexColor(1,1,1,1)
			if SCDB[module.name]["Gradient"] == true then
				button.bg:SetGradientAlpha("VERTICAL", .4, .4, .4, 1, 1, 1, 1, 1)
			end
		end
		
		local name, _, texture, count, dtype, duration, expiration = UnitAura(header:GetAttribute("unit"), button:GetID(), header:GetAttribute("filter"))
		
		if(name) then
			button.texture:SetTexture(texture)
			button.texture:SetTexCoord(0.08, 0.92, 0.08, 0.92)
			button.count:SetText(count > 1 and count or "")
			button.expiration = expiration - GetTime()
			
			if(header:GetAttribute("filter") == "HARMFUL") then
				local color = DebuffTypeColor[dtype] or DebuffTypeColor.none
				button.bg:SetVertexColor(color.r, color.g, color.b)
			else
				button.bg:SetVertexColor(1,1,1,1)
			end
			if SCDB[module.name]["Gradient"] == true then
				button.bg:SetGradientAlpha("VERTICAL", .4, .4, .4, 1, 1, 1, 1, 1)
			end
		end
	end

	local function ScanAuras(self, event, unit)
		if(unit) then
			if(unit ~= PlayerFrame.unit) then return end
			if(unit ~= self:GetAttribute("unit")) then
				self:SetAttribute("unit", unit)
			end
		end
		
		for index = 1, 32 do		
			local child = self:GetAttribute("child" .. index)
			if(child) then
				UpdateAuras(self, child)
			end
		end
	end

	local TimeSinceLastUpdate = 1
	local function CheckWeapons(self, elapsed)
		TimeSinceLastUpdate = TimeSinceLastUpdate + elapsed
		
		if (TimeSinceLastUpdate >= 1) then
			local e1, e1time, _, e2, e2time, _, e3, e3time, _  = GetWeaponEnchantInfo()
			
			local w1 = self:GetAttribute("tempEnchant1")
			local w2 = self:GetAttribute("tempEnchant2")
			local w3 = self:GetAttribute("tempEnchant3")

			if w1 then UpdateWeapons(w1, "MainHandSlot", e1, e1time) end
			if w2 then UpdateWeapons(w2, "SecondaryHandSlot", e2, e2time) end
			if w3 then UpdateWeapons(w3, "RangedSlot", e3, e3time) end

			TimeSinceLastUpdate = 0
		end
	end

	local function CreateAuraHeader(filter, ...)
		local name	
		if filter == "HELPFUL" then name = "PlayerBuffs" else name = "PlayerDebuffs" end

		local header = CreateFrame("Frame", name, UIParent, "SecureAuraHeaderTemplate")
		header:SetPoint(...)
		header:SetClampedToScreen(true)
		header:SetMovable(true)
		header:HookScript("OnEvent", ScanAuras)
		
		header:SetAttribute("unit", "player")
		header:SetAttribute("sortMethod", "TIME")
		header:SetAttribute("template", "AuraTemplate")
		header:SetAttribute("filter", filter)
		header:SetAttribute("point", "TOPRIGHT")
		header:SetAttribute("minWidth", 300)
		header:SetAttribute("minHeight", 94)
		header:SetAttribute("xOffset", -35)
		header:SetAttribute("wrapYOffset", -45)
		header:SetAttribute("wrapAfter", 9)
		header:SetAttribute("maxWraps", 2)
		
		-- look for weapons buffs
		if filter == "HELPFUL" then
			header:SetAttribute("includeWeapons", 1)
			header:SetAttribute("weaponTemplate", "AuraTemplate")
			header:HookScript("OnUpdate", CheckWeapons)
		end
		
		header:Show()
		
		return header
	end

	ScanAuras(CreateAuraHeader("HELPFUL", "TOPRIGHT", BuffFrame, 0,0))
	ScanAuras(CreateAuraHeader("HARMFUL", "TOPRIGHT", BuffFrame, "BOTTOMRIGHT", 0, -50))

	-- create our aura
	local start = CreateFrame("Frame")
	start:RegisterEvent("VARIABLES_LOADED")
	start:SetScript("OnEvent", function(self)
		local frames = {PlayerBuffs, PlayerDebuffs}
		for i = 1, getn(frames) do
			local frame = frames[i]
			local position = frame:GetPoint()
			if position:match("TOPLEFT") or position:match("BOTTOMLEFT") or position:match("BOTTOMRIGHT") then
				frame:SetAttribute("point", position)
			end
			if position:match("LEFT") then
				frame:SetAttribute("xOffset", 36)
			end
			if position:match("BOTTOM") then
				frame:SetAttribute("wrapYOffset", 68)
			end
		end
	end)
	
end
tinsert(SuperClassic.modules, module) -- finish him!	